package com.zklh.leetcode.interviewTop150;

/**
 * 长度最小的子数组
 *
 * @Author tongnan
 * @Date 2024/6/28
 * @Version 1.0
 */
public class Solution209 {
    public int minSubArrayLen(int target, int[] nums) {
        int left = 0, right = 0;
        int sum = 0, minLen = Integer.MAX_VALUE;
        while (right < nums.length || sum >= target) {
            if (sum >= target) {
                if (minLen > right - left) {
                    minLen = right - left;
                }
                sum -= nums[left++];
            } else {
                sum += nums[right++];
            }
        }
        return Integer.MAX_VALUE == minLen ? 0 : minLen;
    }

    public static void main(String[] args) {
        Solution209 solution209 = new Solution209();
        int target = 213;
        int[] nums = {12,28,83,4,25,26,25,2,25,25,25,12};
        System.out.println(solution209.minSubArrayLen(213, nums));
    }
}